package com.dk.service.impl;

import com.dk.mapper.NewsMapper;
import com.dk.pojo.News;
import com.dk.pojo.NewsExample;
import com.dk.service.NewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    private NewsMapper newsMapper;

    @Override
    public PageInfo<News> selectInfo(News news,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
       /* NewsExample ne = new NewsExample();*/

        /*NewsExample.Criteria criteria = ne.createCriteria();
        if (news.getNewsTitle() != null && news.getNewsTitle() !=""){
            criteria.andNewsTitleLike(news.getNewsTitle());
            list = newsMapper.selectByExampleWithBLOBs(ne);
        }else {
            list = newsMapper.selectByExampleWithBLOBs(null);
        }*/
        List<News> list = newsMapper.selectInfo(news);
        PageInfo<News> page = new PageInfo<>(list);
        return page;
    }
    @Override
    public Integer insertInfo(News news) {
        return newsMapper.insert(news);
    }
    @Override
    public Integer deleteById(Integer newsId) {
        return newsMapper.deleteByPrimaryKey(newsId);
    }

    @Override
    public News selectById(Integer newsId) {
        return newsMapper.selectByPrimaryKey(newsId);
    }

    @Override
    public Integer updateInfo(News news){
        int i = newsMapper.updateByPrimaryKey(news);
        return i;
    }

    @Override
    public Integer deleteByIds(Integer[] array) {
        NewsExample ne = new NewsExample();
        NewsExample.Criteria criteria = ne.createCriteria();
        criteria.andNewsIdIn(Arrays.asList(array));
        return newsMapper.deleteByExample(ne);
    }

    @Override
    public List<News> getInfoClient(News news) {
        return newsMapper.selectInfo(news);
    }

    @Override
    public PageInfo<News> getInfoServer(News news, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        List<News> list = newsMapper.selectInfo(news);
        PageInfo<News> page = new PageInfo<>(list);
        return page;
    }

    @Override
    public Integer deleteBatchs(Integer[] array) {
        NewsExample ne = new NewsExample();
        NewsExample.Criteria criteria = ne.createCriteria();
         criteria.andNewsIdIn(Arrays.asList(array));
        return newsMapper.deleteByExample(ne);

    }


}
